package com.zhenyi.jiahua.index.admin;

import com.cjw.model.DBMap;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.jiahua.common.model.JhStaff;
import com.zhenyi.jiahua.core.utils.ImportUtils;
import com.zhenyi.jiahua.index.JHController;
import com.zhenyi.jiahua.task.JHAppointmentTask;
import com.zhenyi.websocket.core.ws.WSServer;

public class MessageController extends JHController {

	// @Override
	// public void index() {
	// String wsuid = Redis.use().get("tmpPush");
	// WSServer wsServer = WSServer.instance();
	// // wsServer.send("json", wsuid);
	// wsServer.send("json");
	// renderSuccess();
	// }

	public void send() {
		Long staffId = getParaToLong("staffId");
		String content = getPara("content");

		JhStaff staff = JhStaff.dao.findById(staffId);
		// DBMap map = new DBMap();
		// map.put("abc", "def");
		// String json = JsonKit.toJson(map);
		// WSServer.instance().sendByToken(staff.getToken(), json);
		// renderSuccess();

		DBMap map = new DBMap();
		map.put("title", "系统消息");
		map.put("content", content);
		map.put("code", 102);
		String json = JsonKit.toJson(map);
		WSServer.instance().sendByToken(staff.getToken(), json);
		renderSuccess();
	}

	public void debug() {
		Cache cache = Redis.use();
		int size = cache.hkeys(WSServer.WS_FIELD).size();
		renderSuccess(size);
	}

	public void list() {
		JhStaff staff = getStaff();
		staff.setMessageCount(0);
		staff.update();
		Kv kv = Kv.create();
		kv.set("staffId", staff.getId());
		SqlPara sqlPara = Db.use("jiahua").getSqlPara("JHFindMessage", kv);
		Page<Record> pages = Db.use("jiahua").paginate(getPage(), PAGE_SIZE, sqlPara);
		setRainbow(pages);
		setItems(jsonObject(pages.getList()));
		render("/jh/admin/message/index.html");
	}

	public void trinoti() {
		new JHAppointmentTask().run();
		renderSuccess();
	}

	// public void imp() {
	// new ImportUtils().go();
	// renderSuccess();
	// }
}
